package com.example.huichaoliu;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.huichaoliu.adapters.ActivityShareAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.rederxu.tools.Dialog;
import com.rederxu.tools.StringUtils;
import com.source.utils.Utils;

public class AddReceptAddressActivity extends BaseActivity implements OnClickListener {
	
	private View layout_commen_address = null;
	private ImageView ic_commen_address = null;
	private EditText et_name = null;
	private EditText et_phone = null;
	private EditText et_address = null;
	private EditText et_postcode = null;
	private String address_id;
	private ImageView ic_delete = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_add_recept_address);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		case R.id.right_button:
			String name = et_name.getText().toString();
			String phone = et_phone.getText().toString();
			String address = et_address.getText().toString();
			String postcode = et_postcode.getText().toString();
			boolean iscommon = layout_commen_address.getTag() != null;
			if(StringUtils.isBlank(name)){
				showToast("请输入收件人姓名");
				return;
			}
			if(StringUtils.isBlank(phone)){
				showToast("请输入收件人电话号码");
				return;
			}
			if(StringUtils.isBlank(address)){
				showToast("请输入收件人详细地址");
				return;
			}
			try{
				JSONObject jo = new JSONObject();
				jo.put("userId", Config.getUserId(this));
				jo.put("name", name);
				jo.put("phone", phone);
				jo.put("address", address);
				jo.put("postcode", postcode);
				jo.put("iscommon", iscommon);
				String url = Config.REQUEST_URLS.URL_USER_ADD_ADDRESS_INFO;
				if(address_id != null){
					url = Config.REQUEST_URLS.URL_USER_MODIFY_ADDRESS_INFO;
					jo.put("addressId", address_id);
				}
				Bundle bundle = new Bundle();
				bundle.putString("request", Utils.requestParamsEncode(jo));
				HTTPRequest.POST(this, url, bundle, new onRequestOverListener() {
					@Override
					public void onRequestOver(String result) {
						try{
							JSONObject jo = new JSONObject(result);
							showToast(jo.getString("message"));
							if(jo.getInt("code") != 0){
								return;
							}
							setResult(RESULT_OK);
							finish();
						}catch(JSONException e){
							e.printStackTrace();
						}
					}
				});
			}catch(Exception e){
				e.printStackTrace();
			}
			break;
		case R.id.tv_commen_address:
		case R.id.ic_commen_address:
			if(layout_commen_address.getTag() == null){
				layout_commen_address.setTag("");
				ic_commen_address.setImageResource(R.drawable.c02);
			}else{
				layout_commen_address.setTag(null);
				ic_commen_address.setImageResource(R.drawable.c01);
			}
			break;
		case R.id.ic_delete:
			Dialog.showMessageDialog(this, "删除地址", "确定删除本地址吗？", "删除", "取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(which == DialogInterface.BUTTON_POSITIVE){
						try{
							JSONObject jo = new JSONObject();
							jo.put("userId", Config.getUserId(mContext));
							jo.put("addressId", address_id);
							Bundle bundle = new Bundle();
							bundle.putString("request", Utils.requestParamsEncode(jo));
							HTTPRequest.POST(mContext, Config.REQUEST_URLS.URL_USER_DELETE_ADDRESS_INFO, bundle, new onRequestOverListener() {
								@Override
								public void onRequestOver(String result) {
									try{
										JSONObject jo = new JSONObject(result);
										showToast(jo.getString("message"));
										if(jo.getInt("code") != 0){
											return;
										}
										setResult(RESULT_OK);
										finish();
									}catch(JSONException e){
										e.printStackTrace();
									}
								}
							});
						}catch(Exception e){
							
						}
					}
				}
			});
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("添加收货地址");
		findViewById(R.id.left_button).setOnClickListener(this);
		findViewById(R.id.tv_commen_address).setOnClickListener(this);
		layout_commen_address = findViewById(R.id.layout_commen_address);
		ic_commen_address = (ImageView) findViewById(R.id.ic_commen_address);
		et_name = (EditText) findViewById(R.id.et_name);
		et_phone = (EditText) findViewById(R.id.et_phone);
		et_address = (EditText) findViewById(R.id.et_address);
		et_postcode = (EditText) findViewById(R.id.et_postcode);
		ic_delete = (ImageView) findViewById(R.id.ic_delete);
		Button right_button = (Button)findViewById(R.id.right_button);
		right_button.setText("保存");
		right_button.setVisibility(View.VISIBLE);
		right_button.setOnClickListener(this);
		ic_delete.setOnClickListener(this);
		ic_commen_address.setOnClickListener(this);
	}

	@Override
	public void initData() {
		String data = getIntent().getStringExtra("data");
		if(data != null){
			try{
				JSONObject jo = new JSONObject(data);
				et_name.setText(jo.getString("name"));
				et_address.setText(jo.getString("address"));
				et_phone.setText(jo.getString("phone"));
				et_postcode.setText(jo.getString("postcode"));
				address_id = jo.getString("addressId");
				ic_delete.setVisibility(View.VISIBLE);
			}catch(JSONException e){
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

}
